/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.program;

import net.bioteam.cloud.cluster.ActiveCluster;
import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.LinkLabel;
import net.bioteam.appweb.gwt.apps.client.Util.UploadItemCallback;
import net.bioteam.appweb.gwt.apps.client.Util.Util;
import net.bioteam.appweb.gwt.apps.clientandserver.Constant;
import net.bioteam.appweb.gwt.apps.clientandserver.ServerException;
import net.bioteam.appweb.gwt.apps.clientandserver.user.UserAction;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class ProgramFormHolder extends VLayout
{

	ProgramForm form;
	
	public ProgramFormHolder()
	{
		
		super();
		setHeight100();
		
		ToolStrip bar = new ToolStrip();
		bar.setWidth100();
		bar.setHeight(20);
		bar.setLayoutLeftMargin(200);

//		LayoutSpacer space1=new LayoutSpacer();
//		space1.setWidth(200);
//		space1.setHeight(20);
//		//space1.setBorder("5px solid blue");
//		bar.addChild(space1);
		
		LinkLabel freerun=new LinkLabel("Run Free");
		freerun.setWidth(60);
		freerun.setPrompt("run in free cluster");
		freerun.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				if(form.validate())
				{
					//set up the call function for UploadItem.
					UploadItemCallback.setCallback();
					form.submitForm();
				}
			}
		});
		bar.addMember(freerun);

		LinkLabel clusterrun = new LinkLabel("Run In Dedicated Cloud Cluster");
		clusterrun.setWidth(200);
		clusterrun.setPrompt("run in a dedicate EC2 cluster");
		bar.addMember(clusterrun);
		clusterrun.addClickHandler(new ClickHandler()
		{
			
			public void onClick(ClickEvent event)
			{
				if(!form.validate())
				{
					return;
				}
				ApplicationCallBack<ActiveCluster[]> callback = new ApplicationCallBack<ActiveCluster[]>()
				{

					public void onFailureAfter(Throwable caught)
					{
						if (caught instanceof ServerException)
						{
							ServerException se=(ServerException)caught;
							if (se.getCode()==ServerException.NO_EC)
							{
								SC.warn("You do not have EC2 Credential. Please go to account management attach EC2 credential to you acocunt first", StaticHolder.dumpB);
								return;
							} 
						}
						SC.warn(caught.getMessage(), StaticHolder.dumpB);
					}

					public void onSuccessAfter(ActiveCluster[] acs)
					{
						ClusterSelectionWindow win=new ClusterSelectionWindow(form);
						win.setActiveCluster(acs);
						win.show();
					}

					public void execute()
					{
						UserAction.Util.getInstance().getActiveCluster(false,this);
					}

				};
				callback.execute();
			}
		});
		

		LinkLabel save = new LinkLabel("Save");
		save.setWidth(50);
		save.setPrompt("save as template");
		save.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				form.setValue(Constant.program_form_action_name, Constant.save_action);
					UploadItemCallback.setCallback();
					form.submitForm();
			}
		});
		bar.addMember(save);
		
		

		LinkLabel wsdl = new LinkLabel("wsdl");
		wsdl.setWidth(50);
		bar.addMember(wsdl);
		wsdl.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				Util.open( "/"+Constant.APPWEB_CONTEXT+"/apps/user/wsdl?program=" + form.getBean().getProgram().getName());
			}
		});

		addMember(bar);
		
	}
	


	public void setProgramForm(ProgramForm f)
	{
		if (form!=null)
		{
			
			removeMember(form);
		}
		
		form=f;
		addMember(form);	
	}
	

	
}
